package com.zhytech.Tuling.Service;

import com.zhytech.Tuling.Beans.TuliingRespone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * Created by zhy on 2017/3/23.
 */
@Service
public class TulingService {

    private static final Logger log = LoggerFactory.getLogger(TulingService.class);
    private static String APIkey = "3d959839f2cb4cbfaed25afc256b0f71";
    private static String TulingApiUrl = "http://www.tuling123.com/openapi/api";

    public String TulingTalk(String info){
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<String, String>();
        params.put("key", APIkey);
        params.put("info", "今天天气怎么样?");
        params.put("loc", "北京市太阳宫");
        params.put("userid", "12341234");

        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String,String>();
        requestEntity.add("key",APIkey);
        requestEntity.add("info",info);
        requestEntity.add("loc","北京市");
        requestEntity.add("userid","123456");
        String response;
        response = restTemplate.postForObject(TulingApiUrl, requestEntity, String.class);
        return response;

    }

    public TuliingRespone TulingTalk2(String info){
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<String, String>();
        params.put("key", APIkey);
        params.put("info", "今天天气怎么样?");
        params.put("loc", "北京市太阳宫");
        params.put("userid", "12341234");

        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String,String>();
        requestEntity.add("key",APIkey);
        requestEntity.add("info",info);
        requestEntity.add("loc","北京市");
        requestEntity.add("userid","123456");
        TuliingRespone tuliingRespone = new TuliingRespone();
        tuliingRespone = restTemplate.postForObject(TulingApiUrl, requestEntity, TuliingRespone.class);
        return tuliingRespone;

    }

    public TuliingRespone TulingTalk3(String info){
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
//        restTemplate.setMessageConverters(getMessageConverters());
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
//        headers.setAccept(Collections.singletonList(MediaType.ALL));
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String,String>();
        requestEntity.add("key",APIkey);
        requestEntity.add("info",info);
        requestEntity.add("loc","北京市");
        requestEntity.add("userid","123456");
        TuliingRespone tuliingRespone = new TuliingRespone();
//      ResponseEntity<TuliingRespone> tuliingResponeResponseEntity =
//                restTemplate.exchange(TulingApiUrl, HttpMethod.GET, entity, TuliingRespone.class, requestEntity);
        tuliingRespone = restTemplate.getForObject(TulingApiUrl + "?key={key}&info={info}&userid=1231234",
                TuliingRespone.class, APIkey,info);
        return tuliingRespone;

//        return tuliingResponeResponseEntity.getBody();

    }

    private static List<HttpMessageConverter<?>> getMessageConverters() {
        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(new MappingJackson2HttpMessageConverter());
        return converters;
    }

    public static void main(String args[]){

        TulingService t = new TulingService();

        String s = t.TulingTalk("煎豆腐怎么做?");
        log.info(s);
        log.info("=====================");
        TuliingRespone tulingRespsone = t.TulingTalk3("宫保鸡丁怎么做?");
        log.info(tulingRespsone.toString() );




    }

}

